package com.huatai.datacenter.controller;

import com.huatai.common.api.R;
import com.huatai.datacenter.entity.ServerObjectEntity;
import com.huatai.datacenter.service.ServerObjectService;
import com.huatai.datacenter.utils.KeyGeneratorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author Lion
 * @date 2023/6/19  20:56
 */

@RequestMapping("/serverObject")
@RestController
@Api(value = "服务对象管理", tags = "服务对象管理")
public class ServerObjectController extends BaseController<ServerObjectService, ServerObjectEntity> {

	@ApiOperation("新增或更新服务对象")
	@Override
	public R save(@ApiParam(required = true) @RequestBody ServerObjectEntity data) {

		if (data.getId() == null){
			String secretKey = null;
			try {
				secretKey = KeyGeneratorUtils.generateAllKey();
			} catch (Exception e) {
				e.printStackTrace();
			}
			data.setSecretKey(secretKey);

			//生成appKey
			data.setAppKey(String.valueOf(System.currentTimeMillis()));
		}

		this.service.SaveOrUpdateWithValid(data);
		return R.success("保存成功");
	}
}
